<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
		<meta http-equiv="X-UA-Compatible" content="ie=edge">
		<title>店铺信息</title>
		<link rel="stylesheet" href="../css/common.css">
		<link rel="stylesheet" href="../css/page.css">
		<script src="../js/jquery-3.2.1.min.js"></script>
		<script src="../js/media.js"></script>
   		<script src="../js/exif.js"></script>
		<script src="../js/common.js"></script>
		<style type="text/css">
			.Personal-certification textarea {
	            box-sizing: border-box;
	            padding: .2rem;
	            height: 1.14rem;
	            width: 100%;
	            border: none;
	            color: #333;
	            font-size: 14px;
	        }
	        .parn{
	            background: #fff;
	            padding: 10px 0;
	            box-sizing: border-box;
	            margin-bottom: 10px;
	        }
	        .add_img{
                width: 2rem;
			    height: 2rem;
			    display: block;
			    margin: 0 auto;
	        }
	        .img-div{
	            position: relative;
	        }
	        .files{
	            position: absolute;
	            left: 0;
	            right: 0;
	            top: 0;
	            bottom: 0;
	            opacity: 0;
	            width: 100%;
	        }
	        h5{
	            padding: 0 .2rem;
	            box-sizing: border-box;
	            margin: 10px 0;
	        }
	        .remarks{
	            text-align: center;
	            margin: 5px 0;
	        }
	        .add_img11{
	        	display: block;
	        	margin: 0 auto;
	        	width: 100%;
	        }
		</style>
	</head>
	<body>
		<div class="Personal-certification bc-gray" style="height: initial;">
	        <div>
	            <h6>店铺设置</h6>
	            <ul>
	                <li>
	                    <span style="width:20vw;font-size: 0.16rem;">店铺名称</span>
	                    <input type="text" style="margin-left:0;font-size: 0.16rem;" placeholder="请输入店铺名称" id="store_name">
	                </li>
	                <li>
	            </ul>
	        </div>
	        <div class="parn">
	            <h6>店铺logo</h6>
	            <div class="img-div">
	                <img src="" class="add_img" id="store_img">
	                <input type="file" class="files" onchange="selectFileImage(this,3)">
	                <div class="remarks">上传店铺logo（尺寸：X*X）</div>
	            </div>
	        </div>
	        <div class="parn">
	        	<h6>店铺宣传海报</h6>
	            <div class="img-div">
	                <img src="../img/上传照片@2x.png" class="add_img11" id="store_img">
	                <input type="file" class="files" onchange="updataimg(this)">
	                <div class="remarks">海报大小请限制在1M以内</div>
	            </div>
	        </div>
	        <div>
	            <p class="p2">店铺介绍</p>
	            <div>
	                <textarea style="border: none;" name="" id="store_des" cols="30" rows="10" placeholder="请输入店铺的介绍"></textarea>
	            </div>
	        </div>
	        
	        <div class="button" onclick="postData_befor()">保存</div>
		</div>
		<script type="text/javascript">
			var storeStatus = getQueryString('is_person_store')=='true'?1:2;//店铺类型1个人2企业
			var upDataimg = '';//上传的logo
			var upDataInf;//上传的数据；
			var dataurl = '';//上柴地址
			var imgNum = 3;//图片位置
			var placard = '';
			//初始化页面
			$.ajax({
				url:base_url + 'ZITAOHUI/store/displayAuthenticationInformation',//获取店铺基本信息
				data: {
					id:getQueryString('is_person_store')=='true'?sessionStorage.getItem("u_id"):sessionStorage.getItem("s_id"),//店铺id
					value:storeStatus
				},
				success:function(data){
					$('#store_name').val(data.extend.data.store_name);//店铺名称
					$('#store_img').attr('src',data.extend.data.store_image);//店铺logo
					$('#store_des').val(data.extend.data.store_describe);//店铺介绍
					if(data.extend.data.placard){//企业海报
						$(".add_img11").attr('src',data.extend.data.placard);
						placard = data.extend.data.placard;
					}
					if(getQueryString('is_person_store')=='true'){//个人
						upDataimg = data.extend.data.IdCardPositive+','+data.extend.data.IdCardBack+','+data.extend.data.footIdCard+','+data.extend.data.store_image;
						upDataInf = {
							userRelName: data.extend.data.user_rel_Name,
							userIdCard: data.extend.data.user_IdCard,
							userPhone: data.extend.data.user_Phone,
							user_Id: sessionStorage.getItem("u_id"),
							images:upDataimg,
							store_id:sessionStorage.getItem("s_id"),
							store_name: data.extend.data.store_name,
							store_describe:  data.extend.data.store_describe,
							placard:data.extend.data.placard
						}
					}else{
						upDataimg = data.extend.data.legal_person_idcard_Positive+','+data.extend.data.legal_person_idcard_Back+','+data.extend.data.Id_cardPositive+','+data.extend.data.license+','+data.extend.data.store_image;
						upDataInf = {//qiye 
							enterprise_name: data.extend.data.enterprise_name,
							legal_person_name: data.extend.data.legal_person_name,
							enterprise_IdCard: data.extend.data.enterprise_IdCard,
							images:upDataimg,
							store_Id: sessionStorage.getItem("s_id"),
							store_name: $('#store_name').val(),
							store_describe: $('#store_des').val(),
							placard:data.extend.data.placard
						}
					};
				}
			});
			function updata(){
				if(storeStatus==1){//判断是个人店铺还是企业店铺
					dataurl =  'ZITAOHUI/store/person/authentication';
					upDataInf.store_name =$('#store_name').val();
					upDataInf.store_describe = $('#store_des').val();
					upDataInf.images = upDataimg;
					upDataInf.placard = placard;//店铺的海报宣传
				}else{
					dataurl = 'ZITAOHUI/store/enterprise/authentication';
					upDataInf.store_name=$('#store_name').val(),
					upDataInf.store_describe=$('#store_des').val(),
					upDataInf.images=upDataimg;
					upDataInf.placard = placard;//店铺的海报宣传
				};				
			};
			function postData_befor(){//保存点击事件
				if($('#store_name').val()==''||$('#store_des').val()==''){
					alert('请输入必填信息')
					return;
				};
				updata();
				$.ajax({
					url: base_url + dataurl,
					type: "post",
					data: upDataInf,
					success:function(data){
						if(data.code==1){
							alert('修改成功');
							window.location.href="setting-center.html"
						}
					}
				});
			};
			function updataimg(fileObj){
				var file_img = fileObj.files['0'];
			    //创建读取文件的对象  
			    var reader = new FileReader();  
			    //创建文件读取相关的变量  
			    var imgFile;  
			    //为文件读取成功设置事件  
			    reader.onload=function(e) {  
				    //alert('文件读取完成');  
				    imgFile = e.target.result;  //这里就已经是base64的格式了
				    console.log(imgFile); 
				    var newsIndex = imgFile.indexOf(",");
					var news = imgFile.substr(parseInt(newsIndex + 1));
					console.log(news);
					$.ajax({
						type: "post",
						url: base_url + "ZITAOHUI/upload/image",
						data: {
							"imageData": news
						},
						dataType: "json",
						success: function(data) {
							if(data.code == 1) {
								var _url = data.extend.msg.fileUrl;
								$('.add_img11').attr('src',_url);
								placard = _url;
							} else {
								alert("上传失败，请刷新后重试");
							}
						},
						error: function(error) {
	
						}
					});
			    };
			    //console.log(file_img.size);
			    if(file_img.size>1024*1024){
			    	alert('图片大小大于1M，请压缩图片后重试');
			    }else{
			    	//正式的读取文件
			    	reader.readAsDataURL(file_img); 
			    }
			    
			}
			
			function selectFileImage(fileObj, ins) {//上传照片
				var obj = fileObj;
				var file = fileObj.files['0'];
				//console.log(file.size);
				//图片方向角 added by lzk
				var $par = $(obj).siblings('.add_img'); //获取方便的img标签，然后回调成功后让其显示图片！
				var Orientation = null;
				if(file) {
					//            layer.open({type: 2});
					//获取照片方向角属性，用户旋转控制
					EXIF.getData(file, function() {
						// //alert(EXIF.pretty(this));
						EXIF.getAllTags(this);
						////alert(EXIF.getTag(this, 'Orientation'));
						Orientation = EXIF.getTag(this, 'Orientation');
						//return;
					});
					var oReader = new FileReader();
					oReader.onload = function(e) {
						//var blob = URL.createObjectURL(file);
						//_compress(blob, file, basePath);
						var image = new Image();
						image.src = e.target.result;
						image.onload = function() {
							var imgWidth = this.width;
							var imgHeight = this.height;
							// 控制上传图片的宽高
							if(imgWidth > imgHeight && imgWidth > 750) {
								imgWidth = 750;
								imgHeight = Math.ceil(750 * this.height / this.width);
							} else if(imgWidth < imgHeight && imgHeight > 1334) {
								imgWidth = Math.ceil(1334 * this.width / this.height);
								imgHeight = 1334;
							}
							var canvas = document.createElement("canvas"),
								ctx = canvas.getContext('2d');
							canvas.width = imgWidth;
							canvas.height = imgHeight;
							if(Orientation && Orientation != 1) {
								switch(Orientation) {
									case 6: // 旋转90度
										canvas.width = imgHeight;
										canvas.height = imgWidth;
										ctx.rotate(Math.PI / 2);
										// (0,-imgHeight) 从旋转原理图那里获得的起始点
										ctx.drawImage(this, 0, -imgHeight, imgWidth, imgHeight);
										break;
									case 3: // 旋转180度
										ctx.rotate(Math.PI);
										ctx.drawImage(this, -imgWidth, -imgHeight, imgWidth, imgHeight);
										break;
									case 8: // 旋转-90度
										canvas.width = imgHeight;
										canvas.height = imgWidth;
										ctx.rotate(3 * Math.PI / 2);
										ctx.drawImage(this, -imgWidth, 0, imgWidth, imgHeight);
										break;
								}
							} else {
								ctx.drawImage(this, 0, 0, imgWidth, imgHeight);
							}
							base64 = canvas.toDataURL("image/jpeg", 0.8);
							//console.log("base64的转换：" + base64);
							var newsIndex = base64.indexOf(",");
							var news = base64.substr(parseInt(newsIndex + 1));
							//console.log("传输给后台的图片数据：" + news);
							$.ajax({
									type: "post",
									url: base_url + "ZITAOHUI/upload/image",
									//url:"http://192.168.1.139:8080/Online/upload/image",
									data: {
										"imageData": news
									},
									dataType: "json",
									success: function(data) {
										if(data.code == 1) {
											var _url = data.extend.msg.fileUrl;
											$('#store_img').attr('src',_url);
											var arr =new Array()
											arr = upDataimg.split(',');
											arr[imgNum] = data.extend.msg.fileUrl;
											upDataimg = arr.join(',');
										} else {
											alert("上传失败，请刷新后重试");
										}
									},
									error: function(error) {
		
									}
							});
						};
					};
					oReader.readAsDataURL(file);
				}
			}
		</script>
	</body>
</html>
